/*
 * VentCambiosPro.java
 *
 * Created on 2 de septiembre de 2006, 12:16
 */

package administrador;
import java.sql.*;
import javax.swing.*;

/**
 *
 * @author  usuario
 */
public class VentCambiosPro extends javax.swing.JFrame {
    JTable tabla;
    Conexion conexion;
    JScrollPane js;
    int bodega;
    
    
    /** Creates new form VentCambiosPro */
    public VentCambiosPro(Conexion c,int BEnt) {
        bodega=BEnt;
        conexion=c;
        initComponents();
        setBounds(150,50,750,559);
        setVisible(true);
        ponerFecha("");
        muestra();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        combo_d = new javax.swing.JComboBox();
        combo_m = new javax.swing.JComboBox();
        combo_a = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Cambios del Mes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(102, 51, 0))); // NOI18N
        jPanel1.setLayout(null);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setForeground(new java.awt.Color(102, 51, 0));
        jLabel2.setText("productos");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(70, 30, 60, 30);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Cambios");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 20, 110, 20);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setForeground(new java.awt.Color(153, 153, 153));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Año");
        jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.add(jLabel3);
        jLabel3.setBounds(130, 20, 70, 20);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setForeground(new java.awt.Color(153, 153, 153));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Mes");
        jLabel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.add(jLabel4);
        jLabel4.setBounds(200, 20, 100, 20);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setForeground(new java.awt.Color(153, 153, 153));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Dia");
        jLabel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.add(jLabel5);
        jLabel5.setBounds(300, 20, 60, 20);

        combo_d.setBackground(new java.awt.Color(240, 240, 240));
        combo_d.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        combo_d.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_dActionPerformed(evt);
            }
        });
        jPanel1.add(combo_d);
        combo_d.setBounds(300, 40, 60, 30);

        combo_m.setBackground(new java.awt.Color(240, 240, 240));
        combo_m.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }));
        jPanel1.add(combo_m);
        combo_m.setBounds(200, 40, 100, 30);

        combo_a.setBackground(new java.awt.Color(240, 240, 240));
        combo_a.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020" }));
        jPanel1.add(combo_a);
        combo_a.setBounds(130, 40, 70, 30);

        jButton1.setText("Mostrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(360, 20, 80, 50);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
       
        muestra();
        
    }//GEN-LAST:event_jButton1ActionPerformed
public void muestra(){
        String fecha=tomaFecha();
        ResultSet respuesta;
        
        respuesta=conexion.consultarBase("select * from modpro where month(fecha)=month('"+fecha+"') and year(fecha)=year('"+fecha+"')");
        //Obtengo Filas-Arreglo de Titulos de Columnas
        int filas=conexion.obtenerFilas(respuesta);
        String[] titulosCol=conexion.obtenerColumnas(respuesta);
        
        //Creo la tabla:
        
        CrearTabla(filas,titulosCol);
        llenaTabla(respuesta,titulosCol.length,filas);

}
    private void combo_dActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_dActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_combo_dActionPerformed
    
    /**
     * @param args the command line arguments
     */
    
    
   public void CrearTabla(int filas,String[] titulos){
        
        int columnas=titulos.length;
        String[][] tabla0=new String[filas][columnas];
       
        tabla= new JTable(tabla0,titulos);
        
        //Aqui le especifico para que la tabla acepte valores doubles e int:
        //--------------------------------------------------------------------
        final boolean[] canEdit = new boolean [columnas];
        for(int p=0;p<columnas;p++){
        canEdit[p]=false;
        }
        tabla.setModel(new javax.swing.table.DefaultTableModel(
            tabla0,titulos  
        ) {
           // boolean[] canEdit = new boolean [] { false, false, false, false};

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        //---------------------------------------------------------------------
        
        //remuevo el JScroll para que no se monte cuando cree el nuevo:
        //*************************************************************
        try{
        jPanel1.remove(js);
        }catch(java.lang.NullPointerException e){}
        //*************************************************************
        
        //Pongo en la tabla:
            js = new JScrollPane(tabla);
                        
            js.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(198, 196, 196)));
            js.setBounds(22, 96, 700, 400);
            js.setVisible(true);
            jPanel1.add(js);
            js.updateUI();
    }
   public void llenaTabla(ResultSet r,int columnas,int filas){
        try{
            r.beforeFirst();
            
            int j=0;
            while(r.next()) {
                
                
                for(int i=0;i<columnas;i++) {
                    
                    Object toma=r.getObject(i+1);
                    //System.out.println("toma valor:"+toma+" * fila:"+j+" * column:"+i);
                    try{
                        tabla.setValueAt(toma,j,i);
                    }catch(java.lang.ArrayStoreException e){
                        tabla.setValueAt("null",j,i);
                    }
                }
                
            j++;    
            }
        }catch(SQLException sqle){
           //System.out.println("Error al mostrar los datos");
            System.err.println(sqle);
        }
        
    }
   
   public String tomaFecha(){
    int mes=combo_m.getSelectedIndex()+1;
    String fecha=combo_a.getSelectedItem().toString()+"-"+mes+"-"+combo_d.getSelectedItem().toString();
    //System.out.println("La fecha es:"+fecha);
    return fecha;
}  
   
   public void ponerFecha(String fecha){

        if(fecha.equals("")){
        //Pongo la fecha del sistema    
        fecha=new java.sql.Date(System.currentTimeMillis()).toString();
        String[] s=fecha.split("-");
        combo_a.setSelectedItem(s[0]);
        combo_m.setSelectedIndex(Integer.parseInt(s[1])-1);
        combo_d.setSelectedIndex(Integer.parseInt(s[2])-1);
        }
        else
        {
        //Pongo la fecha a mi eleccion
        String[] s=fecha.split("-");
        combo_a.setSelectedItem(s[0]);
        combo_m.setSelectedIndex(Integer.parseInt(s[1])-1);
        combo_d.setSelectedIndex(Integer.parseInt(s[2])-1);
        
        
        }
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox combo_a;
    private javax.swing.JComboBox combo_d;
    private javax.swing.JComboBox combo_m;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    
}
